package cn.com.bod.fas.common.annotation.validator;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @description 身份证号验证
 * @author zhangchi
 * @date 2019年10月11日 18:24:32
 * @version 1.0.0
 * @Copyright (c) scfhdt
 */
public class IdCardNoValidator implements ConstraintValidator<IdCardNo, String> {

    private Pattern pattern = Pattern.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$");

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return true;
        }
        return pattern.matcher(value).matches();
    }

    @Override
    public void initialize(IdCardNo constraintAnnotation) {

    }
}
